<?php

Yii::import( "application.controllers.FrontendBaseController" );

class AlbumsController extends FrontendBaseController
{
    public function actionIndex( $id )
    {
        $user = $this->getUserModel($id);
        $isOwner = $user->user_id == $this->getUserIdFromSession();
        
        if ($isOwner)
        {
            $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    "Фотоальбомы" => "" );
        }
        else
        {
            $this->breadcrumbs = array( "Главная" => "/",
                                    "Моя страница" => $this->getUserMainPageUrl(),
                                    $user->surname . " " . $user->name => "/user/" . $user->user_id,
                                    "Фотоальбомы" => '' );
        }
        
        
        $albums = PhotoFacade::getUserPhotoAlbums($user->user_id);
        $albumsCount = PhotoFacade::getCountPhotoAlbumsByUser($user->user_id);
        
        $this->render("albums", array(
            'albums'        => $albums,
            'albumsCount'   => $albumsCount,
            'user'          => $user
        ));
        
    }
    
    private function getUserModel($id = null)
    {
        if (!is_null($id))
        {
            $user = UserFacade::getUserById($id);
        }
        else
        {
            $user = UserFacade::getUserById($this->getUserIdFromSession());
        }
        if ($user === null)
        {
            throw new CHttpException( 404, 'The requested page does not exist.' );
        }
        return $user;
    }
    
}
?>
